package com.uwo.house.controller;

import com.uwo.house.beans.User;
import com.uwo.house.beans.request.DesignProductionReq;
import com.uwo.house.beans.request.DesignSchemeReq;
import com.uwo.house.beans.request.MeasureCenterReq;
import com.uwo.house.beans.request.MeasureCenterSurveyReq;
import com.uwo.house.config.JWTAuthenticationToken;
import com.uwo.house.kits.CacheKits;
import com.uwo.house.service.IDesignCenterService;
import com.uwo.house.service.IMeasureCenterService;
import com.uwo.house.util.UtilReturn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by PHS on 2017/7/22.
 * 设计中心Controller
 */
@Controller
@RequestMapping("/designCenter")
public class DesignCenterController {
    @Autowired
    private IMeasureCenterService measureCenterService;

    @Autowired
    private IDesignCenterService designCenterService;

    /**
     * 设计师端显示指派的订单列表
     * @param orderId
     * @param assignDateFromStr
     * @param assignDateToStr
     * @param orderStatus
     * @return
     */
    @RequestMapping(value = "/measureOrderList", method = RequestMethod.GET)
    @ResponseBody
    public Map measureOrderList(@RequestParam(value = "orderId", required = false) String orderId,
                                @RequestParam(value = "assignDateFromStr", required = false) String assignDateFromStr,
                                @RequestParam(value = "assignDateToStr", required = false) String assignDateToStr,
                                @RequestHeader(value = "Authorization") String token,
                                @RequestParam(value = "orderStatus", required = false) Integer orderStatus) {
        Date assignDateFrom = com.uwo.house.util.DateUtils.getDate(assignDateFromStr);
        Date assignDateTo = com.uwo.house.util.DateUtils.getDate(assignDateToStr);
        String[] strs = token.split(" ");
        token = (strs.length == 2) ? strs[1] : null;
        JWTAuthenticationToken jwtToken = (JWTAuthenticationToken) CacheKits.get(token);
        User user = jwtToken.getUser();
        String userId = user.getId();
        List<MeasureCenterReq> orderList = measureCenterService.getMeasureOrderToDesign(orderId, assignDateFrom, assignDateTo, orderStatus, userId);
        Map map = UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, orderList);
        //model.addAttribute("map",map);
        return map;
    }


    /**
     * 设计人员接单接口
     * @param orderId
     * @param userId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/receiveOrder" ,method = RequestMethod.POST)
    public Map receiveOrder(@RequestParam(value = "orderId") String orderId,
                            @RequestParam(value = "userId") String userId){
        int result = designCenterService.reviceOrder(1,orderId,userId);
        if (result == 1){
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "接单成功！");
        } else {
            return UtilReturn.resErrorMap("接单失败！",UtilReturn.ERROR_CODE);
        }
    }

    /**
     * 设计师查看测量数据接口
     * @param orderId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/measureData" ,method = RequestMethod.POST)
    public Map measureData(@RequestParam(value = "orderId") String orderId){
        Map<String,Object> map = new HashMap<String, Object>();
        // 显示测量人员上传的调查信息
        MeasureCenterSurveyReq measureCenterSurveyReq = designCenterService.showFormData(orderId);
        // 设计师端显示测量数据详情
        MeasureCenterReq measureCenterReq = designCenterService.getOrderInfo(orderId);

        map.put("measureCenterSurveyReq",measureCenterSurveyReq);
        map.put("measureCenterReq",measureCenterReq);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, map);
    }

    /**
     * 设计师进入上传方案页面  同时查询订单的详细信息
     * @param orderId
     * @param userId
     * @return
     */
    @RequestMapping(value = "/toUploadPage", method = RequestMethod.POST)
    @ResponseBody
    public Map toUploadPage(@RequestParam("orderId") String orderId,
                            @RequestParam(value = "userId") String userId) {
        MeasureCenterReq measureCenterReq = designCenterService.getOrderSpecialInfo(orderId);
        DesignSchemeReq designSchemeReq = designCenterService.findDesignScheme(orderId,userId);
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("measureCenterReq",measureCenterReq);
        map.put("designSchemeReq",designSchemeReq);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, map);
    }

    /**
     * 设计师上传方案接口
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/uploadDesignScheme" , method = RequestMethod.POST)
    public Map uploadDesignScheme(@RequestParam(value = "orderId") String orderId,
                          @RequestParam(value = "video3D") String video3D,
                          @RequestParam(value = "buildeImg") String buildeImg,
                          @RequestParam(value = "materialImg") String materialImg,
                          @RequestParam(value = "panoramaUrl") String panoramaUrl,
                          @RequestParam(value = "userId") String userId,
                          @RequestParam(value = "type") String type) throws IOException {
        DesignSchemeReq designSchemeReq = new DesignSchemeReq();
        designSchemeReq.setOrderId(orderId);
        designSchemeReq.setBuildeImg(buildeImg);
        designSchemeReq.setMaterialImg(materialImg);
        designSchemeReq.setPanoramaUrl(panoramaUrl);
        designSchemeReq.setUserId(userId);
        designSchemeReq.setVideo3D(video3D);
        int result = 0;
        if ("new".equals(type)) {
            result = designCenterService.addDesignScheme(designSchemeReq);
        } else {
            result = designCenterService.updateDesignScheme(orderId,userId);
        }
        if (result == 1){
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "上传成功！");
        } else {
            return UtilReturn.resErrorMap("上传失败！",UtilReturn.ERROR_CODE);
        }
    }

    /**
     * 设计师删除完成订单
     *
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/deleteOrder", method = RequestMethod.DELETE)
    @ResponseBody
    public Map deleteOrder(@RequestParam(value = "orderId") String orderId,
                           @RequestParam(value = "userId") String userId) {
        int result = designCenterService.deleteOrderStatus(orderId,userId);
        if (result == 1) {
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "删除成功！");
        } else {
            return UtilReturn.resErrorMap("删除失败！",UtilReturn.ERROR_CODE);
        }
    }

    /**
     * 得到所有作品接口
     * @param str
     * @return
     */
    @RequestMapping(value = "/getAllProduction", method = RequestMethod.POST)
    @ResponseBody
    public Map getAllProduction(@RequestParam(value = "str", defaultValue = "") String str) {
        List<DesignProductionReq> allList = designCenterService.getAllProduction(str);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, allList);
    }

    /**
     * 查看所有作品中的作品详情
     * @param id
     * @return
     */
    @RequestMapping(value = "/getProductionDetail", method = RequestMethod.POST)
    @ResponseBody
    public Map getProductionDetail(@RequestParam(value = "id") Integer id) {
        DesignProductionReq designProductionReq = designCenterService.getProductionDetail(id);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, designProductionReq);
    }


    /**
     * 设计师端我的作品显示接口
     * @param token
     * @return
     */
    @RequestMapping(value = "/getProduction", method = RequestMethod.POST)
    @ResponseBody
    public Map getProduction(@RequestHeader(value = "Authorization") String token) {
        String[] strs = token.split(" ");
        token = (strs.length == 2) ? strs[1] : null;
        JWTAuthenticationToken jwtToken = (JWTAuthenticationToken) CacheKits.get(token);
        User user = jwtToken.getUser();
        String userId = user.getId();
        List<DesignProductionReq> allList = designCenterService.getProduction(userId);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, allList);
    }

    /**
     * 设计师端删除我的作品
     * @param id
     * @return
     */
    @RequestMapping(value = "/deleteProduction", method = RequestMethod.DELETE)
    @ResponseBody
    public Map deleteProduction(@RequestParam(value = "id") Integer id) {
        int result = designCenterService.deleteProduction(id);
        if (result == 1) {
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "删除成功！");
        } else {
            return UtilReturn.resErrorMap("删除失败！",UtilReturn.ERROR_CODE);
        }
    }

    /**
     * 设计师进入我的作品修改页面
     * @param id
     * @return
     */
    @RequestMapping(value = "/toProductionModifyPage", method = RequestMethod.POST)
    @ResponseBody
    public Map toProductionModifyPage(@RequestParam(value = "id") Integer id) {
        DesignProductionReq designProductionReq = designCenterService.getProductionById(id);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, designProductionReq);
    }

    /**
     * 设计师上传作品 或者修改作品接口
     * @param designProductionReq
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/uploadMyProduction" , method = RequestMethod.POST)
    public Map uploadMyProduction(@RequestBody DesignProductionReq designProductionReq,
                                  @RequestHeader(value = "Authorization") String token) {
        int result = 0;
        String[] strs = token.split(" ");
        token = (strs.length == 2) ? strs[1] : null;
        JWTAuthenticationToken jwtToken = (JWTAuthenticationToken) CacheKits.get(token);
        User user = jwtToken.getUser();
        String userId = user.getId();
        if (null != designProductionReq) {
            Object id = designProductionReq.getId();
            designProductionReq.setUserId(userId);
            if (null == id) {
                result = designCenterService.insertProduction(designProductionReq);
            } else {
                result = designCenterService.updateProduction(designProductionReq);
            }
            if (result == 1){
                return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "上传成功！");
            } else {
                return UtilReturn.resErrorMap("上传失败！",UtilReturn.ERROR_CODE);
            }
        }
        return null;
    }

}
